<?php

/**
 * This is the model class for table "category".
 *
 * The followings are the available columns in table 'category':
 * @property string $id
 * @property string $name
 * @property string $url_key
 * @property string $parent_id
 * @property string $index
 * @property integer $is_leaf
 * @property integer $enable
 * @property string $last_updated_time
 *
 * The followings are the available model relations:
 * @property Category $parent
 * @property Category[] $categories
 * @property News[] $news
 */
class Category extends CActiveRecord
{
    private $_path = array();
	/**
	 * Returns the static model of the specified AR class.
	 * @return Category the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'category';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, url_key, last_updated_time', 'required'),
			array('is_leaf, enable', 'numerical', 'integerOnly'=>true),
			array('name, url_key', 'length', 'max'=>45),
			array('parent_id, index', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, url_key, parent_id, index, is_leaf, enable, last_updated_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'parent' => array(self::BELONGS_TO, 'Category', 'parent_id'),
			'categories' => array(self::HAS_MANY, 'Category', 'parent_id'),
			'news' => array(self::HAS_MANY, 'News', 'cat_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'url_key' => 'Url Key',
			'parent_id' => 'Parent',
			'index' => 'Index',
			'is_leaf' => 'Is Leaf',
			'enable' => 'Enable',
			'last_updated_time' => 'Last Updated Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('url_key',$this->url_key,true);
		$criteria->compare('parent_id',$this->parent_id,true);
		$criteria->compare('index',$this->index,true);
		$criteria->compare('is_leaf',$this->is_leaf);
		$criteria->compare('enable',$this->enable);
		$criteria->compare('last_updated_time',$this->last_updated_time,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    public function getRootCats(){
        $criteria=new CDbCriteria;
        $criteria->condition='parent_id is null';
        
        return Category::model()->findAll($criteria);
    }
    
    function getChildren($count = 1000)
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
        $criteria=new CDbCriteria;
        if (empty($this->id)) $criteria->addCondition('parent_id IS NULL');
        else $criteria->compare('parent_id',$this->id);
        $criteria->order = '`index`';
        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
            'pagination'    =>    array(
                'pageSize'    =>        $count,
            ),
        ));
    }
    
    function getChilds($id){
        $criteria = new CDbCriteria();
        $criteria->condition='parent_id='.$id;
        
        $childs=Category::model()->findAll($criteria);
        return $childs;
        
    }
    
    function suggest($keyword)
    {
        if (empty($keyword)) {
            return new CArrayDataProvider(array());
        }

        $criteria = new CDbCriteria();
        $criteria->compare('name', $keyword, true);
        $criteria->mergeWith($this->getDbCriteria());

        return new CActiveDataProvider(self::model(), array(
            'criteria'=>$criteria,
        ));
    }
    
    function getPath()
    {
        if (!isset($this->_path)) {
            $path = array();
            $pid = $this->parent_id;

            while (!empty($pid)) {
                $parent = $this->findByPk($this->parent_id);
                array_unshift($path, $parent);
                $pid = $parent->parent_id;
            };

            $this->_path = $path;
        }

        return $this->_path;
    }
    
    function getAllChilds(){
        $criteria = new CDbCriteria();
        $criteria->condition='parent_id is not null';
        
        $childs=Category::model()->findAll($criteria);
        return $childs;
    }
}